package com.wchen.nio.capter3_2;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @作者： wchen
 * @描述：
 * @创建时间： 2018/7/30 20:34
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
public class TimeServerHandle3 extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String byteBuf) throws Exception {
        byte[] data=byteBuf.getBytes();//创建byte数组
//        byteBuf.readBytes(data);//将数据读取到数组中
        //转String
        String str=new String(data,"utf-8");
        System.out.println("服务端收到数据:"+str);
        //返回数据给客户端     Ctrl+Alt+V :自动补全返回值
//        ByteBuf byteBuf1 = Unpooled.copiedBuffer("我已经接受到数据".getBytes());//转byteBuf
        channelHandlerContext.writeAndFlush("我已经接受到数据");//发送给客户端
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        System.out.println("... exceptionCaught");
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("... channelActive3");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        System.out.println("... channelInactive");
    }
}
